/** 
 * @Title: CrawlListCarOfAutohome.java 
 * @Package com.zizo.autohome.listed.crawl 
 * @Description: TODO
 * @author HeYan
 * @date 2016年8月31日 下午3:58:13 
 */
package com.zizo.autohome.listed.crawl;

import java.sql.Connection;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.zizo.autohome.listed.bean.ListCar;
import com.zizo.autohome.listed.dao.ListCarDao;
import com.zizo.core.Crawl;
import com.zizo.core.DAO;
import com.zizo.util.ConnectionPool;
import com.zizo.util.DocUtil;
import com.zizo.util.Util;

/**
 * @ClassName: CrawlListCarOfAutohome
 * @Description: TODO
 * @author HeYan
 * @date 2016年8月31日 下午3:58:13
 * 
 */
public class CrawlListCarOfAutohome extends Crawl {
	ListCarDao listdao=(ListCarDao) this.dao;


	public CrawlListCarOfAutohome(String id, DAO dao) {
		super(id, dao);
		// TODO Auto-generated constructor stub
	}

	public void getItems(String url) {
		// url = http://www.autohome.com.cn/newbrand/sj_2012.html
		Document doc = DocUtil.getDoc(url);
		Elements carlists = doc.select("div.cars-select div.cars-filter span");
		int i = 0;// 0---按时间 1--按级别 2--按品牌
		for (Element carlist : carlists) {
			String link = "http://www.autohome.com.cn/newbrand/" + carlist.select("a").attr("href");
			Document contentDoc = DocUtil.getDoc(link);
			Elements years = contentDoc.select("div.cars-select div.years-filter span a");
			for (Element year : years) {
				String date = year.attr("href");
				String secondType = date.length() > 4 ? Util.getMatched(date, "(.*_)(\\d{1,})(\\.html)").group(2)
						: date;
				String linkUrl = "http://www.autohome.com.cn/newbrand/" + date;
				crawlContent(linkUrl + "-" + i + "-" + secondType);
			}
			i++;
		}
	}

	public void crawlContent(String url) {
		int first_type = Integer.parseInt(url.split("-")[1]);
		int second_type = Integer.parseInt(url.split("-")[2]);
		Document doc = DocUtil.getDoc(url.split("-")[0]);
		Elements carslist = doc.select("div.cars-list div.select-list");
		for (Element car : carslist) {
			String third_type = car.select("h4").text();
			Elements as = car.select("ul.fn-clear li.fn-left a");
			for (Element a : as) {
				int newsid = Integer
						.parseInt(Util.getMatched(a.attr("href").split("/")[5], "[^0-9]").replaceAll("").trim());
				ListCar listCar = new ListCar(newsid, first_type, second_type, third_type);
				System.out.println(listCar);
                listdao.insertData(listCar);
			}
		}
		}

	@Override
	public int getPages(Document doc, String selectStr) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	public static void main(String[] args) {
		Connection currConn = ConnectionPool.getConnection("autohome_other", "carvoice.tpddns.cn", "3308");
		ListCarDao dao=new ListCarDao(currConn);
		CrawlListCarOfAutohome listcar=new CrawlListCarOfAutohome(null, dao);
		listcar.getItems("http://www.autohome.com.cn/newbrand/sj_2008.html");
	}

}
